function endOfDrag(event) {
    var _this = event.currentTarget;
    _this.dragMessage.dragging = false;
    if (_this.dragMessage.lock) {
        let pos = {
            x: _this.dragMessage.lockPlace.x,
            y: _this.dragMessage.lockPlace.y,
        };
        if (_this.anime) {
            _this.anime(pos);
        } else {
            _this.x = pos.x;
            _this.y = pos.y;
        }
    }
    globalDrag.dragging = false;
    _this.zIndex = 0;
    _this.father && (_this.father.zIndex = 0);
}
import throttle from "https://cdn.jsdelivr.net/npm/lodash-es/throttle.js";
const globalDrag = {
    dragging: false,
};
// ! 主要属性
function initProto(element) {
    Object.assign(element, {
        dragMessage: {
            lock: false, // 直接赋值为 lock false 可以解除拖动回归原位的效果
            draggable: true, // 直接赋值可以生效
            dragging: false, // 内部属性
            lockPlace: {}, // 内部属性
            center: false,
        },
        // 开启拖动回归原位
        lockDrag: () => {
            element.dragMessage.lock = true;
            element.dragMessage.lockPlace = {
                x: element.x,
                y: element.y,
            };
        },
    });
}
export default function (element) {
    initProto(element);
    element
        .on(
            "pointerdown",
            throttle((event) => {
                if (
                    element.dragMessage.draggable &&
                    globalDrag.dragging === false
                ) {
                    var _this = event.currentTarget;
                    _this.dragMessage.data = event.data;

                    _this.dragMessage.dragging = true;
                    _this.diff = {
                        x: event.data.global.x - _this.x,
                        y: event.data.global.y - _this.y,
                    };
                    if (!_this.startPos)
                        _this.startPos = { x: _this.x, y: _this.y };
                    globalDrag.dragging = true;
                    element.zIndex = 100;
                    element.father && (element.father.zIndex = 100);
                }
            }, 100)
        )
        .on("pointerup", (event) => {
            if (element.dragMessage.draggable && element.dragMessage.dragging)
                endOfDrag(event);
        })
        .on("pointerupoutside", (event) => {
            if (element.dragMessage.draggable && element.dragMessage.dragging)
                endOfDrag(event);
        })
        .on("pointerout", (event) => {
            if (element.dragMessage.draggable && element.dragMessage.dragging)
                Object.assign(element, event.data.global);
        })
        .on("pointermove", (event) => {
            if (element.dragMessage.draggable && globalDrag.dragging) {
                var _this = event.currentTarget;
                if (_this.dragMessage.dragging) {
                    var newPosition = event.data.global;

                    let x = newPosition.x - _this.diff.x;
                    let y = newPosition.y - _this.diff.y;

                    if (_this.dragMessage.center) {
                        x =
                            x +
                            (_this.anchor.x * _this.width * _this.scale.x) / 2;
                        y =
                            y +
                            (_this.anchor.y * _this.height * _this.scale.y) / 2;
                    }
                    _this.x = x;
                    _this.y = y;
                }
            }
        });
}
